package us.icitap.cm.ui.shared;

import java.util.ArrayList;
import java.util.List;

import us.icitap.cm.business.entities.Alias;
import us.icitap.cm.business.entities.Person;
import us.icitap.cm.business.entities.UniqueNumber;
import us.icitap.cm.ui.custom.CustButton;
import us.icitap.cm.ui.custom.CustHorizontalLayout;
import us.icitap.cm.ui.custom.CustListSelect;
import us.icitap.cm.ui.custom.CustTextField;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class AliasField extends CustomField<List<Alias>>{

	private final CustListSelect aliasListSelect = new CustListSelect();
	private final CustTextField aliasTextField = new CustTextField();
	private final CustButton addButton = new CustButton("↓");
	private final CustButton removeButton = new CustButton("→");
	
	private Person person;
	private BeanItemContainer<Alias> beans = new BeanItemContainer<Alias>(Alias.class);
	
	private int  totalAlias = 0;
	
	public AliasField(Person p, String caption) {
		this.person = p;
		
		this.aliasListSelect.setContainerDataSource(beans);
		aliasListSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		aliasListSelect.setItemCaptionPropertyId("alias");
		this.setCaption(caption);
	}
	
	public AliasField(String caption) {
		this.aliasListSelect.setContainerDataSource(beans);
		aliasListSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		aliasListSelect.setItemCaptionPropertyId("alias");
		
		this.setCaption(caption);
	}
	
	public AliasField() {
		this.aliasListSelect.setContainerDataSource(beans);
		aliasListSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		aliasListSelect.setItemCaptionPropertyId("alias");

	}
	
	public void setPerson(Person p){
		this.person = p;
	}
	
	public void _init() {
		this.aliasListSelect.setWidth("150px");
		this.aliasListSelect.setRows(7);
		this.aliasListSelect.setNullSelectionAllowed(false);
		this.addButton.setWidth("2em");
		this.removeButton.setWidth("2em");
		
	}

	@Override
	protected Component initContent() {
		GridLayout hl = new GridLayout(2,2);
		hl.setSpacing(true);
		_init();
		
		hl.addComponent(aliasTextField);
		hl.addComponent(addButton);
		hl.addComponent(aliasListSelect);
		hl.addComponent(removeButton);
		
		hl.setComponentAlignment(removeButton, Alignment.TOP_CENTER);
		
		this.aliasListSelect.setImmediate(true);
		this.aliasTextField.setImmediate(true);
		this.addButton.setImmediate(true);
		this.addButton.addClickListener(new ClickListener() {
			
			@SuppressWarnings("unchecked")
			@Override
			public void buttonClick(ClickEvent event) {

				if(aliasTextField.getValue() != null && !"".equals(aliasTextField.getValue().trim())){
					Alias alias = new Alias();
					alias.setAlias(aliasTextField.getValue().trim());
					alias.setPerson(person);
					alias.setId(totalAlias + 1);
					totalAlias += 1;
					
					beans.addBean(alias);
					if(AliasField.this.getInternalValue() == null){
						AliasField.this.setInternalValue(new ArrayList<Alias>());
					}
					
					
					List<Alias> newValue = new ArrayList<Alias>(AliasField.this.getInternalValue());
					
					newValue.add(alias);
					
					AliasField.this.setInternalValue(newValue);
					aliasTextField.setValue(null);
				}
				
			}
		});
		
		this.removeButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				if(aliasListSelect.getValue() != null){
					aliasListSelect.removeItem(aliasListSelect.getValue());
					AliasField.this.setValue((List<Alias>) aliasListSelect.getItemIds());
				}
				
				
			}
		});
		
		return hl;
	}

	@Override
	public Class<? extends List<Alias>> getType() {
		return  (Class<? extends List<Alias>>) List.class;
	
	}
	
	

	@Override
	protected void setInternalValue(List<Alias> newValue) {
		
		super.setInternalValue(newValue);
		if(newValue == null){
			return;
		}
		
		if(newValue != null && !newValue.isEmpty()){
			this.aliasListSelect.removeAllItems();
			beans.addAll(newValue);
			totalAlias = newValue.size();
		}else{
			this.aliasListSelect.removeAllItems();
			totalAlias = 0;
		}
			
	}
}
